/*
 * @Description: 登陆权限模块
 * @Author: yue
 * @Date: 2022-08-29 19:02:46
 * @LastEditors: yue
 * @LastEditTime: 2022-08-29 21:14:37
 */

import router from '@/router/index'
import store from '@/store/index'

// 进度条插件
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 路由白名单
const whiteList = ['/login', '/404']

// 前置路由守卫
router.beforeEach(async function (to, from, next) {
  NProgress.start() // 进度条开始
  if (store.getters.token) {
    // 判断是否在登录页
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.name) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    // 判断是否在白名单
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done() // 进度条结束（）
})

router.afterEach(function (to, from, next) {
  NProgress.done()
})
